package yur.wedding.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import yur.wedding.common.utils.Constant;
import yur.wedding.modules.sys.Vo.documentVo;
import yur.wedding.modules.sys.entity.SysDocumentEntity;
import yur.wedding.modules.sys.service.SysDocshareService;
import yur.wedding.modules.sys.service.SysDocumentService;

import yur.wedding.utils.PageUtils;
import yur.wedding.utils.R;
import yur.wedding.validator.ValidatorUtils;

import javax.annotation.Resource;


/**
 * 文档管理
 *
 * @author Yur
 * @email ${email}
 * @date 2022-05-12 19:28:31
 */
@RestController
@RequestMapping("sys/sysdocument")
public class SysDocumentController extends AbstractController {
    @Autowired
    private SysDocumentService sysDocumentService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdocument:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysDocumentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:sysdocument:info")
    public R info(@RequestBody String documentId) {


        documentVo vo = sysDocumentService.info(documentId);


        return R.ok().put("Info", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdocument:save")
    public R save(@RequestBody SysDocumentEntity sysDocument) {


        R r = sysDocumentService.savePicture(sysDocument, getUserId());

        return r;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdocument:update")
    public R update(@RequestBody SysDocumentEntity sysDocument) {
        ValidatorUtils.validateEntity(sysDocument);
        sysDocumentService.updateById(sysDocument);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdocument:delete")
    public R delete(@RequestBody Long[] documentIds) {
        sysDocumentService.removeByIds(Arrays.asList(documentIds));

        return R.ok();
    }


    /**
     * 查看文档  接口
     * 1.管理员查看
     * 2.当前人
     */
    @RequestMapping("/infoBeLongTo")
    @RequiresPermissions("sys:sysdocument:infoBeLongTo")
    public R infoBeLongTo(@RequestParam Map<String, Object> params) {

        String state = (String) params.get("state");


        System.out.println("模糊条件:" + params.get("wdbh") + "---" + params.get("wdbt") + "---" + params.get("username") + "---" + params.get("cjrq") + "-@----" + params.get("jsrq") + "--" + state);
        //系统管理员，拥有最高权限
        if (getUserId() == Constant.SUPER_ADMIN) {
            if ("1".equals(state)) {//进入条件查询

                PageUtils page = sysDocumentService.queryPageAdministrator(params);
                return R.ok().put("page", page);
            }
            PageUtils page = sysDocumentService.queryPage(params);
            return R.ok().put("page", page);


        } else {//排除管理员 其他人员需按权限查询

            if ("1".equals(state)) {//进入条件查询

                PageUtils page = sysDocumentService.queryPageholDeristrator(params, getUserId());
                return R.ok().put("page", page);

            } else {
                PageUtils page = sysDocumentService.queryPageholDer(params, getUserId());
                return R.ok().put("page", page);
            }


        }


    }

    /**
     *修改文档状态  -发布
     * @return
     */
    @RequestMapping("/publishDoc")
    @RequiresPermissions("sys:sysdocument:publishDoc")
    public R publishDoc(@RequestBody Long[] documentIds) {

        String state="publishDoc";
        Boolean r=   sysDocumentService.updateDoc(documentIds,state);

        if (r==false){
            return R.error("操作失败");
        }
        return R.ok();
    }
    /**
     *修改文档状态  -作废
     * @return
     */
    @RequestMapping("/cancelDoc")
    @RequiresPermissions("sys:sysdocument:cancelDoc")
    public R cancelDoc(@RequestBody Long[] documentIds) {

        String state="cancelDoc";
        Boolean r=   sysDocumentService.updateDoc(documentIds,state);

        if (r==false){
            return R.error("操作失败");
        }
        return R.ok();
    }


    /**
     *修改文档状态  -删除
     * @return
     */
    @RequestMapping("/deleteDoc")
    @RequiresPermissions("sys:sysdocument:cdeleteDoc")
    public R deleteDoc(@RequestBody Long[] documentIds) {


        Boolean r=   sysDocumentService.deleteDoc(documentIds);

        if (r==false){
            return R.error("操作失败");
        }
        return R.ok();
    }
}
